package ui.activity.profit.presenter;


import javax.inject.Inject;

import base.BaseBiz;
import coom.RetrofitHelp;
import ui.activity.profit.biz.SignBiz;
import ui.activity.profit.contract.SignContract;
import ui.model.IntegralBean;
import ui.model.SignBean;

/**
 * Created by chenjian on 19/2/26
 */

public class SignPresenter implements SignContract.Presenter {

    SignContract.View view;
    SignBiz signBiz;


    @Inject
    public SignPresenter(SignContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        signBiz = (SignBiz) biz;
    }


    @Override
    public void getIntegralInfo(String customId) {
        view.loading(true);
        signBiz.getIntegral(customId, new BaseBiz.Callback<IntegralBean>() {
            @Override
            public void onSuccess(IntegralBean info) {
                view.upDateIntegral(info);
                view.loading(false);
            }

            @Override
            public void onFailure(IntegralBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getSignInfo(String customId, int year, int mouth) {
        view.loading(true);
        signBiz.getSignInfo(customId, year, mouth, new BaseBiz.Callback<SignBean>() {
            @Override
            public void onSuccess(SignBean info) {
                view.upDateUI(info, year, mouth);
                view.loading(false);
            }

            @Override
            public void onFailure(SignBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getSignInfo2(String customId, int year, int month) {
        month = month - 1;
        if (month == 0) {
            year = year - 1;
            month = 12;
        }
        int finalMonth = month;
        int finalYear = year;
        view.loading(true);
        signBiz.getSignInfo(customId, year, month, new BaseBiz.Callback<SignBean>() {
            @Override
            public void onSuccess(SignBean info) {
                view.upDateUI2(info, finalYear, finalMonth);
                view.loading(false);
            }

            @Override
            public void onFailure(SignBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getSignInfo3(String customId, int year, int month) {
        month = month - 1;
        if (month == 0) {
            year = year - 1;
            month = 12;
        }
        int finalMonth = month;
        int finalYear = year;
        view.loading(true);
        signBiz.getSignInfo(customId, year, month, new BaseBiz.Callback<SignBean>() {
            @Override
            public void onSuccess(SignBean info) {
                view.upDateUI3(info, finalYear, finalMonth);
                view.loading(false);
            }

            @Override
            public void onFailure(SignBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void signIn(String customId) {
        view.loading(true);
        String url = RetrofitHelp.BASE_URL + "api/v1/sign/" + customId;
        signBiz.signIn(url, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                view.showMsg("您已经签到成功");
                view.upDateSign();
                view.loading(false);
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }
}
